package com.jadefashion.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.mapper.UserMapper;
import com.jadefashion.pojo.Permission;
import com.jadefashion.pojo.Role;
import com.jadefashion.pojo.User;
import com.jadefashion.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    /*@Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;*/

    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUsername(String username) {
        User user=userMapper.findByUsername(username);
        Set<Role> sets=userMapper.findRolesByUserId(user.getId());
        for (Role role : sets) {
           Set<Permission> permissions= userMapper.findPermissonByRoleId(role.getId());
           role.setPermissions(permissions);
        }
        user.setRoles(sets);
        return user;
    }

    //分页查询用户信息
    @Override
    public Map queryPage(QueryPageBean queryPageBean) {
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询用户数据
        List<User> list = userMapper.queryPage(queryPageBean.getQueryString());
        //使用pageInfo接收
        PageInfo pageInfo = new PageInfo(list);

        //创建一个map集合
        Map map = new HashMap();
        map.put("rows",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    //添加用户信息
    @Override
    public void addUser(List<Integer> roleIds, User user) {
        //若未给user添加role即抛出异常
        if (roleIds.size() == 0 || roleIds == null) {
            throw new RuntimeException(MessageConstant.NO_ADD_ROLE_FOR_USER);
        }
        //添加用户
        //加密密码
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userMapper.addUser(user);
        //添加对应角色
        userMapper.addUserDetail(roleIds, user.getId());
    }

    //删除用户信息
    @Override
    public void deleteUserById(Integer id) {
        //删除前先删除user 详情
        userMapper.deleteUserForUserDetailByUserId(id);
        //删除用户
        userMapper.deleteUserById(id);
    }

    //编辑用户信息
    @Override
    public void updateUser(User user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userMapper.updateUser(user);
    }

    //更新用户对应角色数据
    @Override
    public void updateRoleForUserByDetail(List<Integer> deleteIds, List<Integer> addIds, Integer currentRowId) {
//        执行删除
        if (deleteIds != null && deleteIds.size() > 0) {
            userMapper.deleteRoleForUserByDetail(deleteIds, currentRowId);
        }
//        执行添加
        if (addIds != null && addIds.size() > 0) {
            userMapper.addUserDetail(addIds, currentRowId);
        }
    }

}
